﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;

namespace UniRlv.UI.Main.SystemManage
{
    public partial class FrmAddOrEditLineDevice : UniRlv.UI.Main.DialogBase
    {
        LineDevice info = null;
        List<LineDevice> devices;

        public ProductionLine CurLine { get; set; }

        public FrmAddOrEditLineDevice()
        {
            InitializeComponent();
        }

        private void FrmAddOrEditLineDevice_Load(object sender, EventArgs e)
        {
            lblInfo.Text = CurLine.ToString() + "\r\n负责人：" + CurLine.responser + "\r\n扫码工位数量：" + CurLine.stationQuantity;
            lstPortType.Items.AddRange(Const.GetEnumStringArray<port_type>().ToArray());
            lstOutDeviceType.Items.AddRange(Const.GetEnumStringArray<out_device_type>().ToArray());
            lstInDeviceType.Items.AddRange(Const.GetEnumStringArray<in_device_type>().ToArray());

            lstPortType.SelectedIndex = 0;
            lstInDeviceType.SelectedIndex = 0;
            lstOutDeviceType.SelectedIndex = 0;

            for (int i = 0; i <= CurLine.stationQuantity; i++)
            {
                lstStationNO.Items.Add(i.ToString());
            }

            if (this.Tag == null)
            {
                rbIn.Checked = true;
                lstStationNO.SelectedIndex = 1;
            }
            else
            {
                info = this.Tag as LineDevice;
                ShowInfo(info);

                //工位号和子号不允许修改
                lstStationNO.Enabled = false;
                numSubNO.Enabled = false;
            }

            //查询所有产线设备
            string msg = string.Empty;

            devices = DALOpr.Query<LineDevice>("lineID='" + CurLine.ID + "'", ref msg);

            if (!string.IsNullOrEmpty(msg))
            {
                ShowErrorMessageBox("出现错误", msg);
            }
        }

        private void ShowInfo(LineDevice info)
        {
            txtInitString.Text = info.initString;
            txtName.Text = info.name;
            txtRemarks.Text = info.remarks;

            if (info.IO == (int)input_output.INPUT)
            {
                rbIn.Checked = true;
                lstInDeviceType.SelectedIndex = info.deviceType;
            }
            else
            {
                rbOut.Checked = true;
                lstOutDeviceType.SelectedIndex = info.deviceType;
            }

            lstPortType.SelectedIndex = info.portType;
            lstStationNO.SelectedIndex = info.stationNumber;
            numSubNO.Value = info.subDeviceNumber;
            cbAlarm.Checked = info.alarm == 1;
            cbAutoPick.Checked = info.autoPick == 1;
            cbDeviceState.Checked = info.state == 1;
            cbGetCode.Checked = info.getCode == 1;
            cbTwiceScan.Checked = info.twiceScan == 1;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                ShowWarnMessageBox("提示", "设备名称不能为空！");
                txtName.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtInitString.Text))
            {
                ShowWarnMessageBox("提示", "初始化字符串不能为空！");
                txtInitString.Focus();
                return;
            }

            if (info == null)
            {
                info = new LineDevice();
                info.ID = DALOpr.GetNextObjectID("LineDevice");
                info.lineID = CurLine.ID;
                info.lineInfo = CurLine.ToString();
                info.createTime = DateTime.Now;
                info.creator = PublicObject.CurUser.name;

                info.stationNumber = Convert.ToInt32(lstStationNO.Text);
                info.subDeviceNumber = Convert.ToInt32(numSubNO.Value);

                if (devices !=null && devices.Contains(info))
                {
                    info = null;
                    MessageBox.Show("同一工位不允许添加子号相同的同类型设备！");
                    return;
                }
            }

            info.alarm = cbAlarm.Checked ? 1 : 0;
            info.autoPick = cbAutoPick.Checked ? 1 : 0;
            if (rbIn.Checked)
            {
                info.deviceType = lstInDeviceType.SelectedIndex;
            }
            else
            {
                info.deviceType = lstOutDeviceType.SelectedIndex;
            }

            info.getCode = cbGetCode.Checked ? 1 : 0;
            info.initString = txtInitString.Text;
            info.IO = rbIn.Checked ? (int)input_output.INPUT : (int)input_output.OUTPUT;
            info.name = txtName.Text;
            info.portType = lstPortType.SelectedIndex;
            info.remarks = txtRemarks.Text;
            info.state = cbDeviceState.Checked ? 1 : 0;
            info.twiceScan = cbTwiceScan.Checked ? 1 : 0;

            this.Tag = info;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void rbIn_CheckedChanged(object sender, EventArgs e)
        {
            lstInDeviceType.Visible = true;
            lstOutDeviceType.Visible = false;
            
        }

        private void rbOut_CheckedChanged(object sender, EventArgs e)
        {
            lstInDeviceType.Visible = false;
            lstOutDeviceType.Visible = true;
        }

        private void lstStationNO_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
